package combat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JOptionPane;

/**
 *
 * @author Lord Jeff LaJoie
 */

public class KeyBindingMenu extends javax.swing.JFrame {
	
	// up, down, left, right, fire
	//  0 ,  1  ,  2  ,  3  ,  4
	
	KeyBindingCommand kbc1;
	KeyBindingCommand kbc2;
	
	private int[] p1OriginalCommands = new int[5];
	private int[] p2OriginalCommands = new int[5];
	
	private String[] p1OriginalStrings = new String[5];
	private String[] p2OriginalStrings = new String[5];
	
	public int[] p1Cmds = new int[5];
	public int[] p2Cmds = new int[5];
	
	private String[] p1Strings = new String[5];
	private String[] p2Strings = new String[5];
	
	private final int[] p1DefaultCmds = {87, 90, 68, 65, 81};
	private final int[] p2DefaultCmds = {38, 40, 39, 37, 16};
	
	private final String[] p1DefaultStrings = {"w", "z", "d", "a", "q"};
	private final String[] p2DefaultStrings = {"Up", "Down", "Right", "Left", "Shift"};

    /**
     * Creates new form KeyBindingFrame
     */
	public KeyBindingMenu(KeyBindingCommand kbc1, KeyBindingCommand kbc2){
		this.kbc1 = kbc1;
		this.kbc2 = kbc2;
		for(int i=0; i<5; i++){
    		p1OriginalCommands[i] = p1DefaultCmds[i];
    		p2OriginalCommands[i] = p2DefaultCmds[i];
    		
    		p1Cmds[i] = p1OriginalCommands[i];
    		p2Cmds[i] = p2OriginalCommands[i];
    		
    		p1OriginalStrings[i] = p1DefaultStrings[i];
    		p2OriginalStrings[i] = p2DefaultStrings[i];
    		
    		p1Strings[i] = p1DefaultStrings[i];
    		p2Strings[i] = p2DefaultStrings[i];
    	}
		initComponents();
	}

    private void initComponents() {
        keyLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        forwardLabel = new javax.swing.JLabel();
        reverseLabel = new javax.swing.JLabel();
        leftLabel = new javax.swing.JLabel();
        rightLabel = new javax.swing.JLabel();
        fireLabel = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        p1Label = new javax.swing.JLabel();
        p1ForwardButton = new javax.swing.JToggleButton();
        p1ReverseButton = new javax.swing.JToggleButton();
        p1LeftButton = new javax.swing.JToggleButton();
        p1RightButton = new javax.swing.JToggleButton();
        p1FireButton = new javax.swing.JToggleButton();
        jSeparator6 = new javax.swing.JSeparator();
        player2Label = new javax.swing.JLabel();
        p2LeftButton = new javax.swing.JToggleButton();
        p2FireButton = new javax.swing.JToggleButton();
        p2RightButton = new javax.swing.JToggleButton();
        p2ReverseButton = new javax.swing.JToggleButton();
        p2ForwardButton = new javax.swing.JToggleButton();
        jSeparator5 = new javax.swing.JSeparator();
        okButton = new javax.swing.JButton();
        defaultButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        keyLabel.setText("Key");

        forwardLabel.setText("Forward");

        reverseLabel.setText("Reverse");

        leftLabel.setText("Turn Left");

        rightLabel.setText("Turn Right");

        fireLabel.setText("Fire");

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);

        p1Label.setText("Player 1");

        jSeparator6.setOrientation(javax.swing.SwingConstants.VERTICAL);

        player2Label.setText("Player 2");

        setButtonText();
        
        okButton.setText("Ok");

        defaultButton.setText("Default");

        cancelButton.setText("Cancel");

        ////////////////////////
        //LISTENERS START HERE//
        ////////////////////////
        
        p1ForwardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p1ForwardButton.setSelected(true);
            }
        });
        
        p2ForwardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p2ForwardButton.setSelected(true);
            }
        });
        
        p1ReverseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p1ReverseButton.setSelected(true);
            }
        });
        
        p2ReverseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p2ReverseButton.setSelected(true);
            }
        });
        
        p1LeftButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p1LeftButton.setSelected(true);
            }
        });
        
        p2LeftButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p2LeftButton.setSelected(true);
            }
        });
        
        p1RightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p1RightButton.setSelected(true);
            }
        });
        
        p2RightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p2RightButton.setSelected(true);
            }
        });
        
        p1FireButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p1FireButton.setSelected(true);
            }
        });
        
        p2FireButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	disableSelected();
            	p2FireButton.setSelected(true);
            }
        });
        
        p1ForwardButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p1ForwardButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p1Strings[0] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p1Cmds[0] = e.getKeyCode();
	    				p1ForwardButton.setText(p1Strings[0]);
    				}
    				else{
    					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
    				}
    				p1ForwardButton.setSelected(false);
    			}
    		}
			public void keyTyped(KeyEvent e) {}

			public void keyReleased(KeyEvent e) {}
    	});
        
        p2ForwardButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p2ForwardButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p2Strings[0] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p2Cmds[0] = e.getKeyCode();
	    				p2ForwardButton.setText(p2Strings[0]);
    				}
    				else{
    					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
    				}
    				p1ForwardButton.setSelected(false);
    			}
    		}
			public void keyTyped(KeyEvent e) {}

			public void keyReleased(KeyEvent e) {}
    	});
        
        p1ReverseButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e){
				int keyCode = e.getKeyCode();
				if(isValidKey(keyCode)){
    				p1Strings[1] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
    				p1Cmds[1] = e.getKeyCode();
    				p1ReverseButton.setText(p1Strings[1]);
				}
				else{
					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
				}
				p1ReverseButton.setSelected(false);
			}
			public void keyTyped(KeyEvent e) {}

			public void keyReleased(KeyEvent e) {}
    	});
        
        p2ReverseButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p2ReverseButton.isSelected()){

    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p2Strings[1] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p2Cmds[1] = e.getKeyCode();
	    				
	    				p2ReverseButton.setText(p2Strings[1]);
	    				p2ReverseButton.setSelected(false);
    			}
				else{
					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
				}
    				p2ReverseButton.setSelected(false);
    			}
    		}
			public void keyTyped(KeyEvent e) {}

			public void keyReleased(KeyEvent e) {}
    	});
        
        p1LeftButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p1LeftButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p1Strings[3] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p1Cmds[3] = e.getKeyCode();
	    				
	    				p1LeftButton.setText(p1Strings[3]);
	    				p1LeftButton.setSelected(false);
    			}
				else{
					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
				}
    			p2ReverseButton.setSelected(false);
    			}	
    		}
			public void keyTyped(KeyEvent e) {}

			public void keyReleased(KeyEvent e) {}
    	});
        
        p2LeftButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p2LeftButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p2Strings[3] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p2Cmds[3] = e.getKeyCode();
	    				
	    				p2LeftButton.setText(p2Strings[3]);
	    				p2LeftButton.setSelected(false);
    				}
					else{
						JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
					}
    				p2LeftButton.setSelected(false);
    			}	
    		}
			public void keyTyped(KeyEvent e) {}

			public void keyReleased(KeyEvent e) {}
    	});
        
        p1RightButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p1RightButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p1Strings[2] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p1Cmds[2] = e.getKeyCode();
	    				
	    				p1RightButton.setText(p1Strings[2]);
	    				p1RightButton.setSelected(false);
        			}
    				else{
    					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
    				}
    				p1RightButton.setSelected(false);
        			}	
        		}
    			public void keyTyped(KeyEvent e) {}

    			public void keyReleased(KeyEvent e) {}
        	});
        
        p2RightButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p2RightButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p2Strings[2] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p2Cmds[2] = e.getKeyCode();
	    				
	    				p2RightButton.setText(p2Strings[2]);
	    				p2RightButton.setSelected(false);
        			}
    				else{
    					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
    				}
    				p2RightButton.setSelected(false);
        			}	
        		}
    			public void keyTyped(KeyEvent e) {}

    			public void keyReleased(KeyEvent e) {}
        	});
        
        p1FireButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p1FireButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p1Strings[4] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p1Cmds[4] = e.getKeyCode();
	    				
	    				p1FireButton.setText(p1Strings[4]);
	    				p1FireButton.setSelected(false);
        			}
    				else{
    					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
    				}
    				p1FireButton.setSelected(false);
        			}	
        		}
    			public void keyTyped(KeyEvent e) {}

    			public void keyReleased(KeyEvent e) {}
        	});
        
        p2FireButton.addKeyListener(new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			if(p2FireButton.isSelected()){
    				int keyCode = e.getKeyCode();
    				if(isValidKey(keyCode)){
	    				p2Strings[4] = handleKeyActions(e.getKeyCode(), e.getKeyChar());
	    				p2Cmds[4] = e.getKeyCode();
	    				
	    				p2FireButton.setText(p2Strings[4]);
	    				p2FireButton.setSelected(false);
        			}
    				else{
    					JOptionPane.showMessageDialog(KeyBindingMenu.this, "Please select a key that is not in use.");
    				}
    				p2FireButton.setSelected(false);
        			}	
        		}
    			public void keyTyped(KeyEvent e) {}

    			public void keyReleased(KeyEvent e) {}
        	});
        
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
            	if(kbc1 == null){
            		System.out.println("hi");
            	}

            	for(int i=0; i<p1Strings.length; i++){
	        		p1OriginalStrings[i] = p1Strings[i];
	        		p2OriginalStrings[i] = p2Strings[i];
	        		
	        		p1OriginalCommands[i] = p1Cmds[i];
	        		p2OriginalCommands[i] = p2Cmds[i];
            	}
            	
            	kbc1.setKeyBinding(p1Cmds);
            	kbc2.setKeyBinding(p2Cmds);
            	
            	kbc1.execute();
            	kbc2.execute();
            	
                setVisible(false);
            }
        });
        
        defaultButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
            	for(int i=0; i<5; i++){
            		p1Cmds[i] = p1DefaultCmds[i];
            		p2Cmds[i] = p2DefaultCmds[i];
            		
            		p1Strings[i] = p1DefaultStrings[i];
            		p2Strings[i] = p2DefaultStrings[i];
            	}
            	setButtonText();
            }
        });
        
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
            	for(int i=0; i<p1Cmds.length; i++){
            		p1Strings[i] = p1OriginalStrings[i];
            		p2Strings[i] = p2OriginalStrings[i];
            		
            		p1Cmds[i] = p1OriginalCommands[i];
            		p2Cmds[i] = p2OriginalCommands[i];
            	}
            	setButtonText();
            	setVisible(false);
            }
        });
        
        //////////////////////
        //LISTENERS END HERE//
        //////////////////////
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator5)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jSeparator1)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(fireLabel)
                                        .addComponent(forwardLabel)
                                        .addComponent(reverseLabel)
                                        .addComponent(leftLabel)
                                        .addComponent(rightLabel)
                                        .addComponent(keyLabel))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(p1Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(p1ForwardButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(p1RightButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(p1FireButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(p1LeftButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(p1ReverseButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(player2Label)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(p2ForwardButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(p2RightButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(p2FireButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(p2LeftButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(p2ReverseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(okButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(defaultButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cancelButton)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(keyLabel)
                    .addComponent(p1Label)
                    .addComponent(player2Label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(forwardLabel)
                            .addGap(18, 18, 18)
                            .addComponent(reverseLabel)
                            .addGap(18, 18, 18)
                            .addComponent(leftLabel)
                            .addGap(18, 18, 18)
                            .addComponent(rightLabel)
                            .addGap(18, 18, 18)
                            .addComponent(fireLabel))
                        .addComponent(jSeparator4))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(p1ForwardButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p1ReverseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p1LeftButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p1RightButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p1FireButton))
                    .addComponent(jSeparator6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(p2ForwardButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p2ReverseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p2LeftButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p2RightButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(p2FireButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(defaultButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(KeyBindingMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(KeyBindingMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(KeyBindingMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(KeyBindingMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            //    new KeyBindingMenu().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton defaultButton;
    private javax.swing.JLabel fireLabel;
    private javax.swing.JLabel forwardLabel;
    private javax.swing.JToggleButton p1ForwardButton;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JLabel keyLabel;
    private javax.swing.JLabel leftLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JToggleButton p1FireButton;
    private javax.swing.JLabel p1Label;
    private javax.swing.JToggleButton p1LeftButton;
    private javax.swing.JToggleButton p1ReverseButton;
    private javax.swing.JToggleButton p1RightButton;
    private javax.swing.JToggleButton p2FireButton;
    private javax.swing.JToggleButton p2ForwardButton;
    private javax.swing.JToggleButton p2LeftButton;
    private javax.swing.JToggleButton p2ReverseButton;
    private javax.swing.JToggleButton p2RightButton;
    private javax.swing.JLabel player2Label;
    private javax.swing.JLabel reverseLabel;
    private javax.swing.JLabel rightLabel;
    // End of variables declaration
    
    //Start of functional methods to reduce repeated code

    /**
     * Handles special characters based on the key code if the code does not 
     * possess an ascii value.
     * 
     * @param ka The key action code received from the listeners.
     * @param c The character passed from the key listener.
     * 
     * @return A string of which key it corresponds to
     * or else returns the key character returned as a string.
     */
    public String handleKeyActions(int ka, char c){
    	switch (ka){
    		case 27:
    			return "Esc";
    		case 112:
    			return "F1";
    		case 113:
    			return "F3";
    		case 114:
    			return "F4";
    		case 115:
    			return "F5";
    		case 116:
    			return "F6";
    		case 117:
    			return "F7";
    		case 118:
    			return "F8";
    		case 119:
    			return "F9";
    		case 120:
    			return "F10";
    		case 121:
    			return "F11";
    		case 122:
    			return "F12";
    		case 8:
    			return "Backspace";
    		case 155:
    			return "Insert";
    		case 127:
    			return "Delete";
    		case 36:
    			return "Home";
    		case 33:
    			return "Pg Up";
    		case 34:
    			return "Pg Dn";
    		case 35:
    			return "End";
    		case 20:
    			return "Caps Lock";
    		case 16:
    			return "Shift";
    		case 17:
    			return "Ctrl";
    		case 524:
    			return "Windows";
    		case 18:
    			return "Alt";
    		case 37:
    			return "Left";
    		case 38:
    			return "Up";
    		case 39:
    			return "Right";
    		case 40:
    			return "Down";
    	}
    	return "" + c;
    }
    
    /**
     * Checks to see if a key is already selected within the key bindings.
     * @param key The key code for the potential new binding.
     * @return Boolean to determine whether the key is used or not, true if not used.
     */
    private boolean isValidKey(int key){
    	for(int i=0; i<p1Cmds.length; i++){
    		if(p1Cmds[i] == key || p2Cmds[i] == key){
    			return false;
    		}
    	}
    	return true;
    }
    
    /**
     * Sets the toggle buttons equal to their corresponding strings.
     */
    private void setButtonText(){
    	p1ForwardButton.setText(p1Strings[0]);
    	p1ReverseButton.setText(p1Strings[1]);
    	p1LeftButton.setText(p1Strings[3]);
    	p1RightButton.setText(p1Strings[2]);
    	p1FireButton.setText(p1Strings[4]);
    	p2ForwardButton.setText(p2Strings[0]);
    	p2ReverseButton.setText(p2Strings[1]);
    	p2LeftButton.setText(p2Strings[3]);
    	p2RightButton.setText(p2Strings[2]);
    	p2FireButton.setText(p2Strings[4]);
    }
    
    /**
     * Disables all of the toggle buttons.
     */
    private void disableSelected(){
    	p1ForwardButton.setSelected(false);
    	p1ReverseButton.setSelected(false);
    	p1RightButton.setSelected(false);
    	p1LeftButton.setSelected(false);
    	p1FireButton.setSelected(false);
    	
    	p2ForwardButton.setSelected(false);
    	p2ReverseButton.setSelected(false);
    	p2RightButton.setSelected(false);
    	p2LeftButton.setSelected(false);
    	p2FireButton.setSelected(false);
    }
}
